<?php

namespace app\index\controller;

use app\common\entity\MywalletLog;
use app\common\entity\User;
use app\common\service\Users\Identity;
use app\common\shop\ConfigModel;
use app\index\model\SiteAuth;
use think\Controller;
use think\facade\Request;
use think\facade\Session;
use think\facade\Cookie;
use app\index\model\Wechat;

class Base extends Common
{

    public $userId;
    public $userInfo;
    public $sn_id;
    public $is_bind;
    public $token;

    public function initialize()
    {
        header("Access-Control-Allow-Origin:*");
        header("Access-Control-Allow-Methods:GET, POST, OPTIONS, DELETE");

        header("Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept-Language, Origin, Accept-Encoding,Accept,Connection,Cookie,token,Access-Token,PHPSESSID,referer,host,content-length");
        /*公众账号
        账号：zhongyiwugou@163.com
        密码：Andy123456
        公众号设置：APPid:wxb0b4d659b6ad8d81
        AppSecret:a5b2fb6d101229537bf89f3014da51cd*/
        $appid = 'wxb0b4d659b6ad8d81';
        $appKey = 'a5b2fb6d101229537bf89f3014da51cd';
        if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false && empty($this->getUserID()) ) {// 是否微信
            $code = Cookie::get('w_code');

//            (new Wechat($appid,$appKey))->GetOpenid();
        }
//        https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxb0b4d659b6ad8d81&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
//        location.href ='https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx72b84bfc1a97137d&redirect_uri=http%3A%2F%2Fxinxinwuyu.com%2Fapi%2FWechatlogin%2Fwe_login&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';

        //$header = Request::instance()->header();
        $header = Request::header();
        if(isset($header['token'])){
            $this->token = $header['token'];
        }
        // 添加新表等信息
        ConfigModel::add();
        $this->checkSite();
        $this->checkLogin();
        parent::initialize();
    }
    protected function getUserID() {
        $identity = new Identity();
        return $identity->getUserId();
    }
    //判断是否登录
    public function checkLogin()
    {

        $identity = new Identity();
        $userId = $identity->getUserId();
        $token = $this->token;
        if(!$userId && $token){
            $userId = User::where('login_time',$token)->value('id');
        }
        if(!$userId){
//            $userId = 215738;
        }
       /* if ($_SERVER['REQUEST_URI'] != '/index.php/index/Rushgoods/goods_detail'
            && $_SERVER['REQUEST_URI'] != '/index.php/index/article/getService'
            && $_SERVER['REQUEST_URI'] != '/index.php/index/mywallet/updMachineGet'
            && $_SERVER['REQUEST_URI'] != '/index.php/index/mywallet/sealUser'
            && $_SERVER['REQUEST_URI'] != '/index.php/index/mywallet/updUserLevel'
            && $_SERVER['REQUEST_URI'] != '/index.php/index/mywallet/aaabbb'
            && $_SERVER['REQUEST_URI'] != '/index.php/index/shop/index'
            && $_SERVER['REQUEST_URI'] != '/index.php/index/shop/goodsIndex'
            && $_SERVER['REQUEST_URI'] != '/index.php/index/shop/detail'
            && $_SERVER['REQUEST_URI'] != '/index.php/index/shop/packageIndex'
            && $_SERVER['REQUEST_URI'] != '/index.php/index/mywallet/getHashRatio'
            && $_SERVER['REQUEST_URI'] != '/index.php/index/Rushgoods/goods_list'
            && $_SERVER['REQUEST_URI'] != '/index.php/index/shop/detailFarm'
        ) {

            if (!$userId) {
                echo json_encode(['code' => 1, 'message' => '登录后可操作', 'url' => 'login']);die;
            }
        }*/
        if (!$userId) {
            echo json_encode(['code' => 10, 'message' => '登录后可操作', 'url' => 'login']);die;
        }
        if ($userId) {

            $this->userId = $userId;
            /*$this->userInfo = $identity->getUserInfo();

            if($this->userInfo->pos == '' || $this->userInfo->pos == null){
                $this->sn_id = '';
                $this->is_bind = false;
            }else{
                $this->sn_id = $this->userInfo->pos;
                $this->is_bind = true;
            }
            Session::set('username', $this->userInfo->nick_name);
            */

            //print_r($this->sn_id);die;
            //$hash_time = $userInfo['hash_time'];
            //$hash_time_day = date('Ymd', $hash_time);
            //$now = date('Ymd');

            /*if ($hash_time_day != $now) {

                $k_everyday = WithdrawRatio::where('key', 'k_everyday')->value('ratio');
                $mywalletModel = new \app\common\entity\Mywallet();
                $mywalletInfo = $mywalletModel->where('user_id', $userId)->find();
                if ($mywalletInfo['cont_value'] > 0) {
                    $mywalletLogModel = new MywalletLog();
                    $upd_num = bcmul($mywalletInfo['cont_value'] - $mywalletInfo['freeze_value'], $k_everyday, 5);
                    if ($upd_num > 0) {

                        $mywalletLogModel->addLog($userId, $upd_num, 'cont_value', '商品广告奖励', 5, 2);
                        $mywalletModel->where('user_id', $userId)->setDec('cont_value', $upd_num);
                        $mywalletModel->getInsRatio($userId, $upd_num,'商品广告奖励');
//                        $mywalletLogModel->addLog($userId, $upd_num, 'fxtc', '贡献值释放', 2, 1);
//                        $mywalletModel->where('user_id', $userId)->setInc('fxtc', $upd_num);
                        User::where('id', $userId)->setField('hash_time', time());
                    }
                }

            }*/
        }
    }

    //检查站点
    public function checkSite()
    {
        $switch = SiteAuth::checkSite();
        if ($switch !== true) {
            if (request()->isAjax()) {
                return json(['code' => 1, 'message' => $switch]);
            } else {
                (new SiteAuth())->alert($switch);
                exit;
            }
        }
    }

    /**
     * ajax 返回
     * @param type $data
     * @param type $info
     * @param type $status
     * @return type
     */
    public function ajaxreturn($data, $info, $status = false)
    {
        return json([
            'status' => $status,
            'info' => $info,
            'data' => $data
        ]);
    }

}
